﻿module core.hgerect;

private	import	std.c.math;

struct hgeRect
{
   public:
	float	x1, y1, x2, y2;

hgeRect	opCall(float _x1, float _y1, float _x2, float _y2) {x1=_x1; y1=_y1; x2=_x2; y2=_y2; bClean=false; return	*this;}
hgeRect	opCall()		{bClean=true;	return	*this;}

	void    Clear() {bClean=true;}
	bool    IsClean()  {return bClean;}
	void	Set(float _x1, float _y1, float _x2, float _y2) { x1=_x1; x2=_x2; y1=_y1; y2=_y2; bClean=false;	}
	void	SetRadius(float x, float y, float r) { x1=x-r; x2=x+r; y1=y-r; y2=y+r; bClean=false; }

	void Encapsulate(float x, float y)
	{
		if(bClean) {
			x1=x2=x;
			y1=y2=y;
			bClean=false;
		} else {
			if(x<x1) x1=x;
			if(x>x2) x2=x;
			if(y<y1) y1=y;
			if(y>y2) y2=y;
		}
	}
	bool TestPoint(float x, float y) 
	{
		if(x>=x1 && x<x2 && y>=y1 && y<y2) return true;
		return false;
	}
	bool Intersect(hgeRect *rect) 
	{
		if(fabs(x1+x2 - rect.x1-rect.x2) < (x2-x1 + rect.x2-rect.x1)) {
			if(fabs(y1+y2 - rect.y1-rect.y2) < (y2-y1 + rect.y2-rect.y1)) return true;
		}
		return false;
	}

private:
	bool	bClean;
}